package com.wwtech.epp.ui.activity.main;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.base.BaseFragment;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.ui.activity.main.component.DaggerInformComponent;
import com.wwtech.epp.ui.activity.main.contract.InformContract;
import com.wwtech.epp.ui.activity.main.module.InformModule;
import com.wwtech.epp.ui.activity.main.presenter.InformPresenter;
import com.wwtech.epp.ui.activity.work.InformListActivity;
import com.wwtech.epp.ui.adapter.main.ConversationListAdapterEx;
import com.wwtech.epp.utils.UIUtils;
import com.wwtech.epp.view.MorePopWindow;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongContext;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imlib.model.Conversation;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.main
 * @Description: $description
 * @date 2017/08/21 11:13:14
 */

public class InformFragment extends BaseFragment implements InformContract.View, Toolbar.OnMenuItemClickListener {

    @Inject
    InformPresenter mPresenter;
    @BindView(R.id.seal_more)
    ImageView sealMore;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.layout_top)
    LinearLayout layoutTop;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.public_inform)
    TextView publicInform;
    private ConversationListFragment mConversationListFragment;
    private boolean isDebug = false;
    private Conversation.ConversationType[] mConversationsTypes = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_inform, null);
        ButterKnife.bind(this, view);
        Bundle mBundle = getArguments();
        initConversationList();
        initView();
        initDataFromLocal();
        sealMore.setVisibility(View.VISIBLE);
        return view;
    }

    private void initView() {
        layoutTop.setPadding(0, (int) (UIUtils.getStatusBarHeight(getActivity()) * 0.8), 0, 0);
        FragmentPagerAdapter mAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return initConversationList();
            }

            @Override
            public int getCount() {
                return 1;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return "";
            }

        };
        viewpager.setAdapter(mAdapter);
    }

    private Fragment initConversationList() {
        if (mConversationListFragment == null) {
            ConversationListFragment listFragment = new ConversationListFragment();
            listFragment.setAdapter(new ConversationListAdapterEx(RongContext.getInstance()));
            Uri uri;
            if (isDebug) {
                uri = Uri.parse("rong://" + getActivity().getApplicationInfo().packageName).buildUpon()
                        .appendPath("conversationlist")
                        .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "true") //设置私聊会话是否聚合显示
                        .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "true")//群组
                        .appendQueryParameter(Conversation.ConversationType.PUBLIC_SERVICE.getName(), "false")//公共服务号
                        .appendQueryParameter(Conversation.ConversationType.APP_PUBLIC_SERVICE.getName(), "false")//订阅号
                        .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "true")//系统
                        .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "true")
                        .build();
                mConversationsTypes = new Conversation.ConversationType[]{Conversation.ConversationType.PRIVATE,
                        Conversation.ConversationType.GROUP,
                        Conversation.ConversationType.PUBLIC_SERVICE,
                        Conversation.ConversationType.APP_PUBLIC_SERVICE,
                        Conversation.ConversationType.SYSTEM,
                        Conversation.ConversationType.DISCUSSION
                };

            } else {
                uri = Uri.parse("rong://" + getActivity().getApplicationInfo().packageName).buildUpon()
                        .appendPath("conversationlist")
                        .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话是否聚合显示
                        .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")//群组
                        .appendQueryParameter(Conversation.ConversationType.PUBLIC_SERVICE.getName(), "false")//公共服务号
                        .appendQueryParameter(Conversation.ConversationType.APP_PUBLIC_SERVICE.getName(), "false")//订阅号
                        .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "true")//系统
                        .build();
                mConversationsTypes = new Conversation.ConversationType[]{Conversation.ConversationType.PRIVATE,
                        Conversation.ConversationType.GROUP,
                        Conversation.ConversationType.PUBLIC_SERVICE,
                        Conversation.ConversationType.APP_PUBLIC_SERVICE,
                        Conversation.ConversationType.SYSTEM
                };
            }
            listFragment.setUri(uri);
            mConversationListFragment = listFragment;
            return listFragment;
        } else {
            return mConversationListFragment;
        }
    }

    @Override
    protected void setupFragmentComponent() {
        DaggerInformComponent
                .builder()
                .appComponent(((AppConfig) getActivity().getApplication()).getApplicationComponent())
                .informModule(new InformModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(InformContract.InformContractPresenter presenter) {
        mPresenter = (InformPresenter) presenter;
    }

    @Override
    protected void initDataFromLocal() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNum", "1");
        map.put("pageSize", "100");
        mPresenter.getNotice(map, ContainValue.userLogin.getData().getUserId() + "");
        Drawable drawable1 = getResources().getDrawable(R.mipmap.news_remind);
        drawable1.setBounds(0, 0, UIUtils.dip2px(20), UIUtils.dip2px(20));//第一0是距左边距离，第二0是距上边距离，40分别是长宽
        publicInform.setCompoundDrawables(drawable1, null, null, null);//只放左边
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public void setNotice() {
        if (ContainValue.notice.getData().size() == 0 || ContainValue.notice == null || ContainValue.notice.getData() == null) {
            publicInform.setVisibility(View.GONE);
            return;
        }
        String noticeContent = "";
        for (int i = 0; i < ContainValue.notice.getData().size(); i++) {
            noticeContent = noticeContent + (i + 1) + "、" + ContainValue.notice.getData().get(i).getTitle() + "    ";
        }
        publicInform.setText(noticeContent);
    }

    @Override
    public void onResume() {
        super.onResume();
        initDataFromLocal();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return false;
    }

    @OnClick({R.id.seal_more, R.id.public_inform})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.seal_more:
                MorePopWindow morePopWindow = new MorePopWindow(getActivity());
                morePopWindow.showPopupWindow(sealMore);
                break;
            case R.id.public_inform:
                startActivity(new Intent(getActivity(), InformListActivity.class));
                break;
            default:
                break;
        }
    }

}